@extends('layouts.header')
@section('content')
	<h2 style="text-align: center;">
		<a>Welcome to {{ $topic->title }}!<a>
		<a>( {{ $topic->description }} )</a>
	</h2>

	@if (Auth::check())
		<h5 style="text-align: center;">
			@if ($not_subbed)
				<form action="{{ route('subscribe',['sub'=>$topic->title]) }}" method="GET">
					<input type="submit" value="Subscribe" class="pull-right btn btn-success" name="submitBtn" onclick="this.disabled=true;this.form.submit();">
				</form><br>
			@else
				<form action="{{ route('unsubscribe',['sub'=>$topic->title]) }}" method="GET">
					<input type="submit" value="Unsubscribe" class="pull-right btn btn-success" name="submitBtn" onclick="this.disabled=true;this.form.submit();">
				</form><br>
			@endif
			<a style="padding-right: 20px;">Subscribers: {{ $topic->subscriptions }}</a>
			<a style="padding-right: 20px;" href="{{ route('new_post',['sub'=>$topic->title])}}">Create new post</a>
			@if (!$not_moderator)
				<a style="padding-right: 20px;" href="{{ route('add_moderator',['sub'=>$topic->title])}}">Add moderator</a>
			@endif
		</h5>
	@endif
	<br>
	<h2>Posts:</h2>
	<form action="{{ route('show_topic',['sub'=>basename($_SERVER['REQUEST_URI'])])}}" method="GET" role="search">
    {{ csrf_field() }}
        <input type="text" name="search" placeholder="Search posts">
        <button type="submit">Search</button>
	</form>
	@if (isset($_GET['search']) and $_GET['search'])
		@foreach($posts as $post)
			@if (Str::contains($post->title, $_GET['search']))
				<h3><a href="{{ route('show_post',['sub'=>$post->subwroteit, 'post_id' => $post->post_id_string]) }} " title="Show post">{{$post->title}} [Comments: {{$post->number_comments}}]</a></h3>
				<br>
			@endif
		@endforeach
	@else
		@foreach ($posts as $post)
			<h3><a href="{{ route('show_post',['sub'=>$post->subwroteit, 'post_id' => $post->post_id_string]) }} " title="Show post">{{$post->title}} [Comments: {{$post->number_comments}}]</a></h3>
			<br>
		@endforeach
	@endif



@endsection()
